import Request from 'request-promise'
const base = 'https://api.weixin.qq.com/sns/'
const api = {
  authorize: 'https://open.weixin.qq.com/connect/oauth2/authorize?',
  getToken: base + 'oauth2/access_token?',
  updateToken: base + 'oauth2/refresh_token?',
  userInfo: base + 'userinfo?'
}

export default class WechatOauth {
  constructor(opts) {
    this.opts = Object.assign({}, opts)
    this.appID = opts.appID
    this.appSecret = opts.appSecret
  }

  async request(options) {
    options = Object.assign({}, options, {json: true})
    try {
      const response = await Request(options)
      return response
    } catch (error) {
      console.error('错误信息:' + error)
    }
  }

  async getAuthorizeURL(scope = 'snsapi_base', target, state) {
    const url = `${api.authorize}appid=${this.appID}&redirect_uri=${target}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`
    return url
  }

  async fetchAccessToken(code) {
    const url = `${api.getToken}appid=${this.appID}&secret=${this.appSecret}&code=${code}&grant_type=authorization_code`
    const data = await this.request({url: url})
    return data
  }

  async getUserInfo(token, openID, lang = 'zh_CN') {
    const url = `${api.userInfo}access_token=${token}&openid=${openID}&lang=${lang}`
    const data = await this.request({url: url})
    return data
  }
}
